package light.head.dao.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.AuditStatus;
import light.head.model.VehicleApply;
import light.head.model.VehicleApplyItem;

import org.springframework.stereotype.Service;

@Service
public class VehicleApplyDAO extends BaseDAO<VehicleApply, Integer>{
	private final static String sql_findAuditStatusBusiness = " FROM AuditStatus a WHERE a.mc = ? AND a.rc = ? AND a.business = ?";
	private final static String sql_findVehicleApplyItem = " FROM VehicleApplyItem ad WHERE ad.vac = ?";
	/***
	 * 根据当前用户查询申请单
	 * @param au 当前用户编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap(Integer cu, Map<String, String> map , PageBean pb){
		if (null == cu) return null;
		
		StringBuffer hql = new StringBuffer("FROM VehicleApply a WHERE a.cu = ?");
		List<Object> list = new ArrayList<Object>();
		
		list.add(cu);
		if (map != null) {
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND a.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND a.ct <= ?");
			}
			if (isNotNull(title)) {
				list.add("%" + title + "%");
				hql.append("AND a.title LIKE ?");
			}
		}	 
		hql.append(" ORDER BY a.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/***
	 * 根据当前用户查询申请单
	 * @param au 当前用户编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap(List<Integer> ocs, Integer rc, Map<String, String> map , PageBean pb){
		if (null == ocs || null == rc) return null;
		
		List<Object> parm = new ArrayList<Object>();	
		List<Object> countParm = new ArrayList<Object>();
		
		StringBuffer temp = new StringBuffer();
		temp.append(" FROM VehicleApply a");
		temp.append(" JOIN UserInfo u ON u.code = a.cu");
		temp.append(" JOIN Organ o ON o.code = a.oc");
		temp.append(" JOIN AuditStatus s on s.mc = a.code and s.beforeStatus = 1 WHERE a.deleted = 0 AND s.business = 3 AND s.rc = ? and a.oc IN (");
		
		parm.add(rc);
		countParm.add(rc);
		for (Integer ig : ocs) {
			temp.append("?,");
			parm.add(ig);
			countParm.add(ig);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			
			if (isNotNull(deleted)) {
				parm.add(Boolean.parseBoolean(deleted));
				countParm.add(Boolean.parseBoolean(deleted));
				temp.append(" AND a.deleted = ?");
			}
			if (isNotNull(time1)) {
				parm.add(DateUtils.getDateByStr(time1, null));
				countParm.add(DateUtils.getDateByStr(time1, null));
				temp.append(" AND a.ct >= ?");
			}
			if(isNotNull(time2)){
				parm.add(DateUtils.getDateByStr(time2, null));
				countParm.add(DateUtils.getDateByStr(time2, null));
				temp.append(" AND a.ct <= ?");
			}
			if(isNotNull(title)) {
				parm.add("%" + title + "%");
				countParm.add("%" + title + "%");
				temp.append(" AND a.title LIKE ?");
			}
		}
		StringBuffer sql = new StringBuffer("SELECT a.code, a.title, a.cu, a.ct, a.oc, a.deleted, u.realName, o.name");
		StringBuffer countSql = new StringBuffer("SELECT COUNT(*)");
		
		sql.append(temp).append(" ORDER BY a.code DESC");
		countSql.append(temp);
		
		if (RESULT.SUCCESS == queryToPageBean(pb, sql.toString(), parm, countSql.toString(), countParm))
			return packageResult(pb);
		else
			return RESULT.ERROR;
		}
	
	
	
	
	
	/***
	 * 封装
	 * @param pb
	 * @return
	 */
	private RESULT packageResult(PageBean pb) {
		
		VehicleApply info = null;
		List<VehicleApply> list = new ArrayList<VehicleApply>();
		
		//  a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name
		for (Object o : pb.getResultList()) {
			
			info = new VehicleApply();
			Object[] obj = (Object[]) o;
			
			Integer code = (null == obj[0] || Sys.NONE.equals(obj[0].toString())) ? null : (Integer) obj[0];
			String title = (null == obj[1] || Sys.NONE.equals(obj[1].toString())) ? null : (String) obj[1];
			Integer cu = (null == obj[2] || Sys.NONE.equals(obj[2].toString())) ? null : (Integer) obj[2];
			Date ct = (null == obj[3] || Sys.NONE.equals(obj[3].toString())) ? null : (Date) obj[3];
			Integer oc = (null == obj[4] || Sys.NONE.equals(obj[4].toString())) ? null : (Integer) obj[4];
			Boolean deleted = (null == obj[5] || Sys.NONE.equals(obj[5].toString())) ? null : (Boolean) obj[5];
			String cn = (null == obj[6] || Sys.NONE.equals(obj[6].toString())) ? null : (String) obj[6];
			String on = (null == obj[7] || Sys.NONE.equals(obj[7].toString())) ? null : (String) obj[7];
			
			info.setCode(code);
			info.setTitle(title);
			info.setCu(cu);
			info.setCt(ct);
			info.setOc(oc);
			info.setDeleted(deleted);
			info.setCn(cn);
			info.setOn(on);
			
			list.add(info);
		}
		pb.setResultList(list);
		return RESULT.SUCCESS;
	}




	/***
	 * 根据当前用户查询申请单
	 * @param au 当前用户编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMapOcs(List<Integer> ocs, Map<String, String> map , PageBean pb){
		if (null == ocs) return null;
		
		StringBuffer hql = new StringBuffer("FROM VehicleApply a WHERE a.designate = 0 AND a.deleted = 0 AND a.verify = 2 AND a.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			String cn = map.get(Parm.CN);
			if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND a.deleted = ?");
			}
			if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND a.ct >= ?");
			}
			if (isNotNull(time2)) {
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND a.ct <= ?");
			}
			if (isNotNull(cn)) {
				hql.append(" AND a.cn LIKE ?");
				list.add("%" + cn + "%");
			}
			if (isNotNull(title)) {
				list.add("%" + title + "%");
				hql.append("AND a.title LIKE ?");
			}
		}	 
		hql.append(" ORDER BY a.code");
		return findToPageBean(hql.toString(), pb, list);
	}

  /***
   * 根据申请单编号查询申请明细
   * @param code
   * @return
   */
	public List<VehicleApplyItem> findApplyItem(Integer code) {
		if (null == code)
			return null;
		List<VehicleApplyItem> list = find(sql_findVehicleApplyItem, code);
		return list;
	}
	/***
	 * 
	 * @param mc
	 * @param rc
	 * @return
	 */
	public List<AuditStatus> findAuditStatusBusiness(Integer mc, Integer rc, Byte business) {
		if (null == mc || null == rc)return null;
		return find(sql_findAuditStatusBusiness, null, null, mc, rc, business);
	}
}
